package edu.nf.api.controller.order;

import com.fasterxml.jackson.databind.util.JSONWrappedObject;
import com.google.gson.Gson;
import edu.nf.api.controller.BaseController;
import edu.nf.meituan.dao.order.user.UserOrderDAO;
import edu.nf.meituan.dao.order.user.impl.UserOrderDAOImpl;
import edu.nf.meituan.model.order.Order;
import edu.nf.meituan.model.order.OrderItem;
import edu.nf.meituan.model.order.user.UserOrder;
import edu.nf.meituan.model.order.user.UserOrdersItem;
import edu.nf.meituan.model.user.Shopping;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import java.util.List;

/**
 * @Date 2023/6/19
 * @Author zgm
 */
public class OrderController extends BaseController {

    @RequestMapping("/user/Order/listOrder")
    public View userOrder(@RequestParam("uid")int id){
        UserOrderDAO dao = new UserOrderDAOImpl();
        List<UserOrder> list = dao.queryUserOrder(id);
        return new JsonView(list);
    }

    @RequestMapping("/user/Order/updateOrderStatus")
    public View updateOrderStatus(@RequestParam("orderId") int orderId,
                                  @RequestParam("status") int status){

        UserOrderDAO dao = new UserOrderDAOImpl();
        int row = dao.updateOrderStatus(orderId, status);
        return new JsonView(row);
    }
    @RequestMapping("/user/Order/listOrdersItem")
    public View listOrdersItem(@RequestParam("orderId")int orderId){
        UserOrderDAO dao = new UserOrderDAOImpl();
        List<UserOrdersItem> list = dao.queryUserOrdersItem(orderId);
        return new JsonView(list);
    }
    @RequestMapping("/user/Order/ConfirmPayment")
    public View addOrderAndOrderItem(@RequestParam("order") String order,
                                     @RequestParam("orderItem") String[] orderItem){
        // 把json字符串中的数据封装到实体对象中
        Order order1 = new Gson().fromJson(order, Order.class);
        UserOrderDAO dao = new UserOrderDAOImpl();
        // 返回受影响行数的自增
        int orderId = dao.addOrder(order1);

        for (int i = 0; i < orderItem.length; i++) {
            // 把购物车的商品转成数组，遍历出来
            // 把json字符串中的数据封装到实体对象中
            OrderItem orderItem1 = new Gson().fromJson(orderItem[i], OrderItem.class);
            // 把受影响行数的自增，进行封装
            orderItem1.setOrderId(orderId);
            // 循环的添加订单商品信息
            dao.addOrderItem(orderItem1);
        }
        return new JsonView(success("下单成功"));
    }
}